package com.example.demo.security.sysFriend.controller;


import com.example.demo.security.sysUser.entity.UserBean;
import com.example.demo.security.sysUser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("friend")
public class FriendController {

    @Autowired
    private UserService userService;

    private final String file_path="security/sysUser/";

    @RequestMapping("toAdd")
    public String toAdd(){
        return file_path+"addFriend";
    }

    @RequestMapping("toMessage")
    public String toMessage(){
        return file_path+"addMessage";
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(HttpServletRequest request,Integer id){
        Integer userId=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            Integer i=userService.addFriend(userId,id);
            if (i==0){
                return "申请失败！";
            }else {
                return "申请成功！";
            }
        }catch (Exception e ){
            e.printStackTrace();
            return "系统异常！";
        }
    }

    @RequestMapping("friendList")
    @ResponseBody
    public List<UserBean> friendList(HttpServletRequest request,String name){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            if (!name.equals("")){
                List<UserBean> list=userService.queryFriendsById(id,name);
                return list;
            }else {
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping("messageList")
    @ResponseBody
    public List<UserBean> messageList(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            List<UserBean> list=userService.queryAddMessageById(id);
            return list;
        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping("agreeMessage")
    @ResponseBody
    public String agreeMessage(HttpServletRequest request,Integer id){
        Integer userId=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            Integer i=userService.agreeMessage(userId,id);
            if (i<=1){
                return "添加失败！";
            }else {
                return "添加成功！";
            }
        }catch (Exception e ){
            e.printStackTrace();
            return "系统异常！";
        }
    }

    @RequestMapping("disagreeMessage")
    @ResponseBody
    public String disagreeMessage(HttpServletRequest request,Integer id){
        Integer userId=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            Integer i=userService.disagreeMessage(userId,id);
            if (i==0){
                return "删除失败！";
            }else {
                return "删除成功！";
            }
        }catch (Exception e ){
            e.printStackTrace();
            return "系统异常！";
        }
    }
}
